import CONSTANT from '@/common/constants.js'
import http from '@/common/http.js'
import Confirm from 'vux/src/components/confirm/index.vue'
import util from '@/common/util.js'

export default {
  name: 'courseDetails',
  components: {Confirm},
  data () {
    return {
      siteId: '',
      // 班级id
      id: '',
      studentId: '',
      studentName: '',
      classNoShow: false,
      content: {
        bean: {
          caption: {}
        }
      },
      isDisabled: false,
      signClass: 'signUpBtn',
      agree: true,
      show: false
    }
  },
  mounted () {
    this.$router.afterEach((to, from, next) => {
      window.scrollTo(0, 0)
    })
    var _this = this
    var studertids = ''
    _this.siteId = window.sessionStorage.getItem('siteId')
    _this.studentId = window.sessionStorage.getItem('studentId')
    _this.id = window.sessionStorage.getItem('event_id')
    let classNoFlag = window.sessionStorage.getItem('classNoFlag')
    if (classNoFlag == 1) {
      _this.classNoShow = true
    }
    studertids = _this.studentId
    if (studertids == '-1') {
      studertids = ''
    }
    http.post(CONSTANT.CLAZZVIEW, {
      orgId: _this.siteId,
      id: _this.id,
      userId: studertids
    }, (res) => {
      let date = new Date()
      let setting = {
        randomTime: util.dateFormat(window.sessionStorage.getItem('randomTime'), true),
        beginTime: util.dateFormat(window.sessionStorage.getItem('beginTime'), true),
        endTime: util.dateFormat(window.sessionStorage.getItem('endTime'), true)
      }
      _this.processStatus(res.data, setting, date)
      _this.content = res.data
      _this.content.bean.beginDate = util.dateFormat(res.data.bean.beginDate,false) + ' 开课'
      let description = _this.content.bean.description
      if (description && description.indexOf('报名协议') !== -1) {
        _this.content.bean.description = description.substring(0, description.indexOf('报名协议'))
      }
      _this.content.bean.ageMin = util.dateFormat(_this.content.bean.ageMin,false)
      _this.content.bean.ageMax = util.dateFormat(_this.content.bean.ageMax,false)
    })
    _this.studentName = window.sessionStorage.getItem('studentName')
    if (_this.studentName == null || _this.studentName == '-1') {
      let params = '?siteId=' + _this.siteId + '&studentId=' + _this.studentId;
      http.get(CONSTANT.STUDENTVIEW + params, {
        siteId: this.siteId,
        studentId: this.studentId
      }, (res) => {
        _this.studentName = res.name
      })
    }
  },
  methods: {
    onCancel () {
      // 取消按钮
    },
    onConfirm () {
      var _this = this
      _this.$router.push({path: '/login'})
    },
    goSignUp () {
      var _this = this
      if (!_this.isDisabled) {
        _this.isDisabled = true
        _this.signClass = 'signUpNotBtn'
        if (_this.studentId == '-1') {
          _this.show = true
          return
        }
        if (_this.agree == false) {
          alert('请同意并接受《少年宫报名协议》！')
        } else {
          // 班级报名
          http.post(CONSTANT.CLASSAPPLY, {
            orgId: _this.siteId,
            classId: _this.id,
            userId: _this.studentId,
            setId: window.sessionStorage.getItem('setId')
          }, (res) => {
            _this.isDisabled = false
            _this.signClass = 'signUpBtn'
            //console.log(res)
            if (res.code != 0) {
              alert(res.msg)
            } else {
              let templateId = window.sessionStorage.getItem('wechatModelId')
              let params2 = '?appId=' + window.sessionStorage.getItem('appId') +
                '&reserveNo=' + res.data.reserveNo +
                '&className=' + _this.content.name +
                '&studentId=' + _this.studentId +
                '&templateId=' + templateId +
                '&beginTime=' + _this.content.classDate +
                '&classTime=' + _this.content.timeable +
                '&studentName=' + window.sessionStorage.getItem('studentName') +
                '&openId=' + window.sessionStorage.getItem('openId')
              //alert(params2)
              if (templateId != null && templateId != '' && templateId != undefined) {
                let params = '?appId=' + window.sessionStorage.getItem('appId') + '&reserveNo=' + res.data.reserveNo
                + '&className=' + _this.content.name + '&studentId=' + _this.studentId + '&templateId=' + templateId
                + '&beginTime=' + _this.content.classDate + '&classTime=' + _this.content.timeable
                +'&studentName=' + window.sessionStorage.getItem('studentName') + '&openId=' + window.sessionStorage.getItem('openId')
                http.get(CONSTANT.PAYMENT_API + '/app/template/wxSend' + params, {
                  appId: window.sessionStorage.getItem('appId'),
                  reserveNo: res.data.reserveNo,
                  className: _this.content.name,
                  studentId: _this.studentId,
                  templateId: templateId,
                  beginTime: _this.content.classDate,
                  classTime: _this.content.timeable
                }, (res2) => {

                })
              }
              if (_this.content.bean.enrollType === '0') {
                // alert(res.tipMessage + '预约号：' + res.reserveNo)
                alert('预约成功！请点击“确定”立即缴费或稍后在“我的班级”逐一缴费！'  + '预约号：' + res.data.reserveNo)
                var query = {
                  paySerial: res.data.paySerial,
                  reserveNo: res.data.reserveNo,
                  name: this.content.bean.name,
                  totalFees: res.data.applyFee,
                  area: this.content.bean.caption.area,
                  classNo: this.content.bean.code,
                  frontStatusName: this.content.statusProperty,
                  year: this.content.bean.year,
                  term: this.content.bean.caption.term,
                  spelName: this.content.bean.spelName,
                  degree: this.content.bean.caption.degree,
                  studentName: this.studentName
                }
                this.$router.push({path: '/payment', query: query})
              } else if (_this.content.bean.enrollType === '1') {
                alert('预约成功！预约号：' + res.data.reserveNo + ',请等待摇号结果公布~~')
              } else if (_this.content.bean.enrollType === '2') {
                alert('预约成功！预约号：' + res.data.reserveNo + ',请耐心等待，面试工作人员将会适时和您取得联系并安排面试~~')              }
            }
          })
        }
      }
    },
    agreeUp (e) {
      var _this = this
      //console.log(e.target.className)
      if (e.target.className == 'iconfont icon-yuanxingxuanzhongfill') {
        e.target.className = 'iconfont icon-yuanxingweixuanzhong'
        _this.agree = false
      } else {
        e.target.className = 'iconfont icon-yuanxingxuanzhongfill'
        _this.agree = true
      }
    },
    comeBack () {
      window.history.go(-1)
    },
    processStatus(data, setting, currentTime) {
      if (data.bean.status == 3 && (data.bean.issueStatus == 1 || data.bean.issueStatus == 4)) {
        if (data.bean.recruitNum - data.bean.reserveNum > data.bean.applyUsedNum) {
          data.canApply = true;
          data.statusProperty = "报名进行中";
        } else {
          data.canApply = false;
          data.statusProperty = "名额已满";
        }
      } else if (data.bean.status == 3 && data.bean.issueStatus == 2) {
        data.canApply = false;
        if (data.bean.enrollType == 0) {
          data.statusProperty = "即报即得";
        } else if (data.bean.enrollType == 1) {
          data.statusProperty = "摇号中";
        } else if (data.bean.enrollType == 2) {
          data.statusProperty = "面试中";
        }
      } else if (data.bean.status == 3 && data.bean.issueStatus == 3) {
        data.canApply = false;
        data.statusProperty = "报名已关闭";
      }
      if (setting.beginTime > currentTime) {
        data.canApply = false;
        data.statusProperty = "报名未开始";
      }
      if (setting.endTime < currentTime) {
        data.canApply = false;
        data.statusProperty = "报名已结束";
      }
      // 是否已经到了预设的摇号时间
      if (setting.randomTime < currentTime) {
        // 摇号的班级进入摇号时间后不得再报名
        if (data.bean.enrollType == 1) {
          data.canApply = false;
          data.statusProperty = "摇号中";
        }
      }
    }
  }
}
